package com.tanhua.recommend.listeners;

import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.mongo.PublishScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "quanzi_recommend_logs",consumerGroup = "quanziRecommendLogsGroup")
public class QuanziLogsListener implements RocketMQListener<Map> {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void onMessage(Map map) {
//        map.put("userId",userId);
//        map.put("movementId",movementId);
//        map.put("type",type);
        Long userId = Long.parseLong(map.get("userId").toString());
        String movementId = (String) map.get("movementId");
        Integer type = (Integer) map.get("type");


        Movement movement = mongoTemplate.findById(new ObjectId(movementId), Movement.class);
        if(movement==null||movement.getPid()==null){
            return;
        }

        PublishScore publishScore = new PublishScore();
        publishScore.setId(new ObjectId());
        publishScore.setUserId(userId);
        publishScore.setDate(new Date().getTime());

        publishScore.setPublishId(movement.getPid());

        Double score = 0.0;

        switch (type){
            case 1:{
                score = 1D;
                break;
            }case 2:{
                score = 5D;
                break;
            }case 3:{
                score = 8D;
                break;
            }case 4:{
                score = 10D;
                break;
            }case 5:{
                score = -5D;
                break;
            }case 6:{
                score = -8D;
                break;
            }default:{
                break;
            }
        }
        publishScore.setScore(score);

        mongoTemplate.save(publishScore);
        System.out.println("publishScore保存成功.............");

    }
}
